home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 105_01.zip / CIO.C < prev    next >
Text File  |  1993-06-09  |  3KB  |  97 lines

  1. /* Special Raw console I/O for BDSC            Steve Ward 1/81
  2.  *
  3.  * Provides a reasonably flexible, device-independent interface:
  4.  *
  5.  *    ch = getchar()        Read a character
  6.  *
  7.  *    putchar(ch)        write a character
  8.  *
  9.  *    kbhit()            true iff input character waiting
  10.  *
  11.  *    old = TTYMode(m)    Sets tty mode bits to m, returning old
  12.  *                 value.  Mode bits:
  13.  *        1    Echo mode: chars echoed as read.
  14.  *        2    Quit: ^C causes an exit.
  15.  *        4    Flow: ^S, ^Q flow control.
  16.  *        8    Strip: input characters stripped to 7 bits.
  17.  *        16    Expand: expand \n, \t on output.
  18.  *
  19.  * Hence for REALLY raw I/O, do TTYMode(0).
  20.  *
  21.  * NOTE: Program using CIO.C are NOT PORTABLE to systems lacking a C compiler!
  22.  *     If you really must use CIO, then compile this once (to get CIO.CRL)
  23.  *     and link your programs by saying:
  24.  *
  25.  *        A>clink main <other CRL files> DEFF CIO <cr>
  26.  *
  27.  */
  28.  
  29. #include "bdscio.h"            /* Make sure BDSCIO.H has been
  30.                        customized for your system!   */
  31.  
  32. /* Device-specific definitions...       Input status flag:        */
  33. #define    ISTAT    ((CIMASK & inp(CSTAT)) == (CAHI ? CIMASK : 0))
  34.                     /* character output status:    */
  35. #define    OSTAT    ((COMASK & inp(CSTAT)) == (CAHI ? COMASK : 0))
  36.  
  37. /* Internal static definitions.                        */
  38. #define    Freeze    (static[1])        /* true if output frozen (^S) */
  39. #define    Pending    (static[2])        /* true if input char waiting */
  40. #define    PendCh    (static[3])        /* the pending input char     */
  41. #define    Mode    (static[4])        /* input mode bits:         */
  42. #define    M_echo    1            /* echo mode bit        */
  43. #define    M_quit    2            /* ^C (quit) mode bit        */
  44. #define    M_flow    4            /* ^S/^Q flow control        */
  45. #define    M_strip    8            /* strip to 7-bits        */
  46. #define    M_expan    16            /* Expand \n on output.        */
  47.  
  48. #define    QuitC    (static[5])
  49.  
  50.  
  51. putchar(c) {  rawio(1, c); }
  52.  
  53. getchar() { return rawio(2); }
  54.  
  55. kbhit() { return rawio(3); }
  56.  
  57. TTYMode(mode) { return rawio(4, mode); } /* set mode bits, returns prev. */
  58.  
  59. rawio(key, arg)
  60.  {    char ch, *static, mode;
  61.     static = "Nonsense!";
  62.  
  63.     if (*static == 'N')        /* Initialization ...    */
  64.      { *static = Freeze = Pending = 0;
  65.        Mode = M_echo | M_quit | M_flow | M_strip | M_expan;
  66.        QuitC = 3; }
  67.  
  68. Again:    if (ISTAT)        /* check for input pending.    */
  69.      { ch = inp(CDATA);
  70.        if ((mode=Mode) & M_strip) ch &= 0x7F;
  71.        if ((mode & M_quit) && (ch == QuitC)) exit();
  72.        else if (mode & M_flow)
  73.         { if (ch == ('S'-64)) { Freeze=1; goto brk; }
  74.           if (ch == ('Q'-64)) { Freeze=0; goto brk; }}
  75.        if (!Pending)
  76.         { Pending = 1;
  77.           PendCh = ch;
  78.           if (mode & M_echo) outp(CDATA, ch); }}
  79.  
  80. brk:    switch(key)
  81.      { case 0:    return;
  82.        case 1:    if (Freeze || !OSTAT) goto Again;  /* putchar(arg) */
  83.             outp(CDATA, arg);
  84.             if ((arg == '\n') && (Mode & M_expan))
  85.                 putchar('\r');
  86.             return arg;
  87.        case 2:    if (!Pending) goto Again;
  88.             Pending = 0; return PendCh;
  89.        case 3:    return Pending;
  90.        case 4:    mode = Mode; Mode = arg;
  91.             if (!(arg & M_flow)) Freeze = 0;
  92.             return mode;
  93.        default:    return;
  94.      }
  95.  }
  96.  
  97.